package com.tuanzi.loan.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

import com.tuanzi.loan.workflow.vo.StarterResponse;

@FacesConverter("starterResponseDualListConverter")
public class StarterResponseDualListConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		DualListModel<StarterResponse> model = (DualListModel<StarterResponse>) ((PickList) component).getValue();
		for (StarterResponse starterResponse : model.getSource()) {
			if (value.equals(starterResponse.getStarter())) {
				return starterResponse;
			}
		}

		for (StarterResponse starterResponse : model.getTarget()) {
			if (value.equals(starterResponse.getStarter())) {
				return starterResponse;
			}
		}

		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		return value.toString();
	}
	// ...
}